
-- @Description: 红包扫雷机器人
local util = require "util"
local skynet = require("skynet")
local Timer = require("Timer")
local math_random = math.random
local table_copy = table.copy
local table_remove = table.remove
local string_split = string.split
local table_insert = table.insert
local math_floor = math.floor

local Robot = class("Robot")

function Robot:ctor(robot_manager)

    self.close = true
    self.robot_manager = robot_manager
    self.fhb = false            --是否发红包
    self.fhb_time = 0           --发红包时间

    self.qhb_index = 0              --抢红包时间帧
    self.qhb = false                --是否抢红包
    self.qhb_time_index = 0         --抢红包时间
    self.sign = false
end

--注册消息
function Robot:register()

    self.robot_manager:registerMessage(self.uid, "table.NTF_PLAYER_ENTER", handler(self, self.playerEnterNtf))
    self.robot_manager:registerMessage(self.uid, "table.NTF_LEAVE", handler(self, self.leaveNtf))
    -- self.robot_manager:registerMessage(self.uid, "table.NTF_SITDOWN", handler(self, self.sitdownNtf))
    -- self.robot_manager:registerMessage(self.uid, "table.NTF_STANDUP", handler(self, self.standUpNtf))

    -- self.robot_manager:registerMessage(self.uid, "table.RSP_STANDUP", handler(self, self.standUpRsp))
    -- self.robot_manager:registerMessage(self.uid, "table.RSP_PLAYER_LIST", handler(self, self.playerListRsp))
    -- self.robot_manager:registerMessage(self.uid, "table.RSP_SITDOWN", handler(self, self.sitdownRsp))   
    self.robot_manager:registerMessage(self.uid, "table.RSP_LEAVE", handler(self, self.leaveRsp))
    -- self.robot_manager:registerMessage(self.uid, "table.RSP_CHAT", handler(self, self.chatRsp)) 
    -- self.robot_manager:registerMessage(self.uid, "table.RSQ_USE_MAGIC_PICTURE", handler(self, self.magicRsp))

    self.robot_manager:registerMessage(self.uid, "hbsl.NTF_WAIT_START", handler(self, self.waitStartNtf))
    self.robot_manager:registerMessage(self.uid, "hbsl.NTF_WAIT_GAMEOVER", handler(self, self.waitGameoverNtf))
    self.robot_manager:registerMessage(self.uid, "hbsl.NTF_START", handler(self, self.startNtf))
    self.robot_manager:registerMessage(self.uid, "hbsl.NTF_GAMEOVER", handler(self, self.gameoverNtf))
    self.robot_manager:registerMessage(self.uid, "hbsl.NTF_ROB_PACKET", handler(self, self.robPacketNtf))
    self.robot_manager:registerMessage(self.uid, "hbsl.NTF_SEND_PACKET", handler(self, self.sendPacketNtf))
    self.robot_manager:registerMessage(self.uid, "hbsl.RSP_SEND_PACKET", handler(self, self.sendPacketRsp))
    self.robot_manager:registerMessage(self.uid, "hbsl.RSP_ROB_PACKET", handler(self, self.robPacketRsp))
        
end

function Robot:init(uid)
    self.close = false
    self.uid = uid  
    self.user_manager = global.user_manager
    self.room_config = global.room_config
    self.rule = global.rule
    self.desk = global.desk
    self.user = self.user_manager:getUser(uid)

    self.server_id = self.room_config:getServerId()
    self.table_id = self.room_config:getTableId()
    self.table_type = self.room_config:getTableType()
    self.table_gid = self.room_config:getTableGid()
    self.game_type = self.room_config:getGameType()
    self.table_pattern = self.room_config:getTablePattern() 
    self.room_data = self.room_config:getRoomData()     
    self.robot_data = self.room_config:getRobotData()

    self.robot_type = self.user:getRobotTypeId() --机器人类型id 1:非R型机器人 2：小R型机器人 3:中R型机器人 4：大R型机器人 5：土豪型机器人
    self.config = self.room_config:getRobotConfig(self.robot_type) --robot.lua配置
    self.life_duration  = math_random(5 * 60, 30 * 60) --机器人生命周期 10分钟~60分钟
    self.start_time = os.time() --开始时间

    self:register()
end

function Robot:clear()
    if self.close then 
        return 
    end             
    self.close = true
    self.robot_manager:removeRobot(self.uid)
    self.user = nil
    self.uid = nil

end

--是否处于关闭中
function Robot:isClose()
    -- dbglog("__加载机器人数量____1111_______1_",self.close)
    return self.close
end

function Robot:getUser()
    return self.user
end

--发消息给桌子
function Robot:send(message_name, data)
    local fd = -1
    -- skynet.send(skynet.self(), "lua", message_name, self.uid, fd, data)
    self.robot_manager:dispatch(message_name, self.uid, fd, data)
end

--一个定时驱动逻辑
function Robot:update() 
    -- errlog("hbsl robot update")
    if self.close then 
        return 
    end

    if #self.desk.hb_list == 0 and self.sign then
        self:send_fhb()
    end

    local curr_time = util.get_now_time()
    if self.desk:getState() == "startgame" then 
        self.qhb_index = self.qhb_index + 1
        if self.qhb and self.qhb_index >= self.qhb_time_index then
            dbglog(self.qhb_index)
            self:send_qhb()
        end
        self.sign = true
    end

    if self.fhb and curr_time >= self.fhb_time then
        self:send_fhb()
    end
end

--抢红包
function Robot:send_qhb()
    self.qhb = false                --是否抢红包
    self.qhb_time_index = 0         --抢红包时间
    dbglog(self.uid,"robot send_qhb")
    self:send("REQ_ROB_PACKET")
end

--发红包
function Robot:send_fhb()
    local coins = 0
    local coins_range = string_split(self.robot_data.hb_coins_rate, ',')
    local hb_coins_list = {}
    local all_weight = 0
    for i,v in pairs(coins_range) do
        local item = string_split(v, '=')
        all_weight = all_weight + tonumber(item[1])
        table_insert(hb_coins_list,{weight = tonumber(item[1]) , coin = tonumber(item[2])})
    end
    local rand_weight = math_random(all_weight)
    all_weight = 0
    for _,v in pairs(hb_coins_list) do
        all_weight = all_weight + v.weight
        if all_weight >= rand_weight then
            coins = v.coin 
            break
        end
    end
    dbglog(hb_coins_list,coins,rand_weight)
    if coins > self.user:getCoins() or coins == 0 or coins < self.room_data.hb_coins_range[1] then
        coins = self.room_data.hb_coins_range[1]
    end

    local lei_number = math_random(0,9)

    self.fhb = false
    self.fhb_time = 0
    self:send("REQ_SEND_PACKET",{coins = coins,thunder_number = lei_number,count = 1})
end

--是否待发红包
function Robot:isfhb()
    return self.fhb 
end

--设置发红包
function Robot:setFhb(time)
    local curr_time = util.get_now_time()
    self.fhb = true
    self.fhb_time = curr_time + math_random(1,time + 4) 

    -- dbglog("hbsl setFhb time:",self.fhb_time)
end

--设置抢红包 type 1 为第一阶段抢  2 为第二阶段抢
function Robot:setQhb(type,time,tb_time)
    -- dbglog("setQhb self.room_data.time",self.room_data.time)
    self.qhb = true
    self.qhb_index = 0              --抢红包时间帧
    if type == 1 then
        local time1 = math_floor(time*2*0.6)
        time1 = time1 > 3 and time1 or 3
        self.qhb_time_index = math_random(3,time1)
    elseif type == 2 then
        local time1 = math_floor(time*2*0.6)+1
        local time2 = time*2
        time1 = time1 > 3 and time1 or 3
        time2 = time2 > time1 and time2 or (time1 + 1)
        self.qhb_time_index = math_random(time1,time2) 
    elseif type == 3 then
        tb_time = tb_time or {}
        local time1 = (tb_time[1] or 2)*2
        local time2 = (tb_time[2] or 5)*2
        time1 = time1 > 3 and time1 or 3
        time2 = time2 > time1 and time2 or (time1 + 1)
        self.qhb_time_index = math_random(time1,time2)
    end
    -- dbglog("setQhb",self.room_data.time,self.qhb_time_index)
    -- self.qhb_time_index = 1
end
-------------

function Robot:playerEnterNtf() 
end
function Robot:leaveNtf(data)   
    -- dbglog("___robot______leaveNtf__", dump(data))
    if data.uid ~= self.uid then 
        return 
    end
    self:clear()
end


function Robot:sitdownNtf() 
end
function Robot:standUpNtf() 
end

--开始
function Robot:startNtf()

end
function Robot:waitStartNtf()
    self.room_data = self.room_config:getRoomData()     
    self.robot_data = self.room_config:getRobotData()
end
--结束
function Robot:gameoverNtf(data)
    self.qhb = false                --是否抢红包
    self.qhb_time_index = 0         --抢红包时间
    self:leaveDesk()
end

function Robot:waitGameoverNtf(data)
end

--抢红包
function Robot:robPacketNtf(data)

end

--发红包
function Robot:sendPacketNtf(data)

end

--发红包返回
function Robot:sendPacketRsp(data)

end

--抢红包返回
function Robot:robPacketRsp(data)

end

function Robot:isBanker()
    return false
end

function Robot:standUpReq() 

end
--坐下
function Robot:sitdownReq(seat_id)  
end

function Robot:leaveReq()       
    -- print("_______Robot leaveReq____")   
    self:send("REQ_LEAVE")
end

--离桌
function Robot:leaveDesk()  
    if self.close then 
        return 
    end 
    if not self.user then 
        return 
    end

    --金币不足时 30%概率离桌 

    if self.user:getCoins() < self.room_data.hb_coins_range[1]*self.room_data.hb_times and math_random(100) < 30 then       
        self:leaveReq() 
        dbglog("机器人金币不足时离桌________",self.uid)
        return 
    end 
    
    --机器人有一定概率的离桌率  
    local leave_rate = self.robot_data.leave_rate or 5
    if math_random(1,100) <= leave_rate then        
        self:leaveReq()        

        dbglog("机器人有一定概率的离桌率", leave_rate)
        return
    end     
    --一定时间内要离桌
    local curr_time = os.time()
    if curr_time - self.start_time > self.life_duration then
        self:leaveReq()        
        self.start_time = curr_time --机器人生命周期 10分钟~60分钟
        dbglog("机器人一定时间内要离桌", self.life_duration, self.uid )
        return
    end
end

function Robot:leaveRsp(data)   
    if not data then        
        return 
    end 
    if data.result == 0 then            
        --可以离开      
        self:clear()        
    end
end

function Robot:leaveNtf(data)   
    if data.uid ~= self.uid then 
        return 
    end
    self:clear()
end

function Robot:getCoins()
    return self.user:getCoins()
end
return Robot